﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using YogaStudio.Service;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private readonly IProductService _productService;
        private readonly IUserManagementService _userManagementService;
        private readonly IOrderService _orderService;

        public ProductController(): this(new ProductService(), new UserManagementService(), new OrderService()) { }

        public ProductController(IProductService productService, IUserManagementService userManagementService, IOrderService orderService)
        {
            _productService = productService;
            _userManagementService = userManagementService;
            _orderService = orderService;
        }

        public ActionResult GetAllProducts(string category, string searchString)
        {
            var productList = _productService.GetAllAvailableProducts(category, searchString);

            return View(productList);
        }

        public ActionResult ProductDetails(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var product = _productService.GetProductyId(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            return View(product);
        }

        [Authorize]
        public ActionResult PurchasedOrder()
        {
            var currentUser = _userManagementService.GetUser(User.Identity.Name);
            var allOrders = _orderService.GetAllOrdersByUserId(currentUser.UserId);
            return View(allOrders);
        }

        [Authorize]
        public ActionResult PurchasedOrderDetails(int orderId)
        {
            var currentUser = _userManagementService.GetUser(User.Identity.Name);
            var orderItems = _orderService.GetAllOrderItems(orderId, currentUser.UserId).ToList();
            return View(orderItems);
        }
	}
}